package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.bean.User;

import java.util.List;

// 定义一个用户登陆类
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //声明控件
    private Button mBtnLogin;
    private Button btn_signin;
    private EditText mEtUser;
    private EditText mEtPassword;
    private EditText etName;
    private MySQLiteOpenHelper nMySQLiteOpenHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //这里是界面打开后 最先运行的地方
        super.onCreate(savedInstanceState);
        //设计加载对应界面UI
        setContentView(R.layout.activity_main);

        nMySQLiteOpenHelper = new MySQLiteOpenHelper(this);
        //找到控件
        mBtnLogin = findViewById(R.id.btn_login);
        mEtUser = findViewById(R.id.et_1);
        mEtPassword = findViewById(R.id.et_2);
        mBtnLogin.setOnClickListener(this);
        btn_signin = findViewById(R.id.btn_signin);
        //匹配对应的用户名和密码才能进行注册操作
        btn_signin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转页面
                Intent intent = new Intent(MainActivity.this,SigninActivity.class);
                startActivity(intent);
            }
        });

    }
    public void query(View view) {
        String name = mEtUser.getText().toString().trim();

        List<User> users = nMySQLiteOpenHelper.queryFromDbByName(name);
    }

    // 定义一个登陆用户名和密码比较的方法
    public boolean login(String username, String password) {
        // 从数据库中查找与输入的用户名相对应的记录
        List<User> users = nMySQLiteOpenHelper.queryFromDbByName(username);

        // 如果找不到与用户名相对应的记录，则登录失败
        if (users == null || users.isEmpty()) {
            return false;
        }

        // 遍历查询结果，查找匹配的用户名和密码
        for (User user : users) {
            if (user.getName().equals(username) && user.getNumber().equals(password)) {
                return true; // 用户名和密码匹配，登录成功
            }
        }

        return false; // 没有匹配的用户名和密码，登录失败
    }


    public void onClick(View v)
    {
        //弹出的内容设置
        String ok = "登录成功!";
        String fall = "密码或账户有误，请重新输入！";
        String username = mEtUser.getText().toString().trim();
        String password = mEtPassword.getText().toString().trim();
        Intent intent = null;

        if (login(username,password)) {
            //普通版
//            Toast.makeText(getApplicationContext(),ok,Toast.LENGTH_SHORT).show();
            intent = new Intent(MainActivity.this, FunctionActivity.class);//跳转页面
            startActivity(intent);//加载新界面
        }
        else {
            //提升版，居中显示
            Toast toastCenter = Toast.makeText(MainActivity.this, fall, Toast.LENGTH_SHORT);
            toastCenter.setGravity(Gravity.CENTER, 0, 0);
            toastCenter.show();
        }

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }

}
